﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jacobi.Vst.Core;

namespace KinectedMusic.Audio
{
    public class Instrument : IInstrument
    {
        private VSTPlugin plugin;
        private Notes notes = new Notes();
        private string pluginName;

        public Notes Notes
        {
            get
            {
                return notes;
            }
        }

        public Instrument(string pluginName)
        {
            this.pluginName = pluginName;
        }

        public void AddNote(byte note, double length, byte velocity)
        {
            notes.AddNote(note, length, velocity);
        }

        public void LoadVST(int bufferSize)
        {
            plugin = new VSTPlugin(pluginName, bufferSize);
            plugin.LoadVST();
        }

        public VstAudioBuffer[] GetOutputBuffers(double position, double barStep, int bufferSize)
        {
            plugin.SendMIDI(notes.GetMIDIEvents(position, barStep, bufferSize));
            return plugin.GetOutputBuffers();
        }

        //TODO: REMOVE!
        public void TestNotes()
        {
            notes.AddNote(60, 2.0, .25);
            notes.AddNote(62, 2.12, .25);
            notes.AddNote(63, 2.66, .25);
            notes.AddNote(67, 3.5, .50);
            notes.AddNote(60, 4.75, 1.0);
            notes.AddNote(70, 6.75, 1.0);
        }

        public void TestNotes1()
        {

            for (double x = 0; x <= 10000; x+=.5)
            {
                notes.AddNote(51, x, .1);
                notes.AddNote(52, x + .125, .1);
            }
            for (double x = .25; x <= 10000; x += .5)
            {
                notes.AddNote(40, x, .1);
                notes.AddNote(42, x + 125, .1);
            }
        }
        public void TestNotes2()
        {
            notes.AddNote(40, 0, 100);
        }
    }
}
